package com.nip.irregularvebsdict.utilities;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Xml;

import com.nip.irregularvebsdict.items.IrregularVerbItem;

public class IrregularVerbsParser extends BaseParserFeed {

	public static final String LIST_TAG = "list";
	public static final String ITEM_TAG = "item";
	public static final String BASE_ATTR = "base";
	public static final String PASS_SIMPLE_ATTR = "pastsimple";
	public static final String PAS_PARTICIPLE_ATTR = "pastparticiple";

	private List<IrregularVerbItem> results = null;
	private IrregularVerbItem item;

	public IrregularVerbsParser(InputStream inputStream) {
		super(inputStream);
	}

	public List<IrregularVerbItem> getIrregularVerbItems() {

		RootElement rootElement = new RootElement(LIST_TAG);
		Element element = rootElement.getChild(ITEM_TAG);
		rootElement.setStartElementListener(new StartElementListener() {

			@Override
			public void start(Attributes attributes) {
				// TODO Auto-generated method stub
				results = new ArrayList<IrregularVerbItem>();
			}
		});

		element.setStartElementListener(new StartElementListener() {

			@Override
			public void start(Attributes attributes) {
				item = new IrregularVerbItem(attributes.getValue(BASE_ATTR),
						attributes.getValue(PASS_SIMPLE_ATTR), attributes
								.getValue(PAS_PARTICIPLE_ATTR));

			}
		});
		element.setEndElementListener(new EndElementListener() {

			@Override
			public void end() {
				results.add(item);
			}
		});

		try {
			Xml.parse(this.inputStream, Xml.Encoding.UTF_8, rootElement.getContentHandler());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return results;
	}
}
